﻿/// We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor elements 
/// located on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. Example:


using System;

class zad3
{
    private static int[,] directions = { { 0, 1 }, { 1, 0 }, { 1, 1 }, { 1, -1 } };
    private static int maxCount = 0;
    private static string maxCountValue;

    static void Main()
    {
        //string[,] matrix = { { "ha", "fifi", "ha", "hi", "uy" }, 
        //                     { "fo", "re", "ha", "xx", "tr" }, 
        //                     { "xxx", "ho", "ha", "xx", "re" },
        //                     { "xxx", "ho", "ew", "ha", "re" }
        //                   };
        string[,] matrix = { { "s", "qq", "s" }, { "pp", "pp", "s" }, { "pp", "qq", "s" } };

       
        bool[, ,] used = new bool[matrix.GetLength(0), matrix.GetLength(1), directions.Length];

        for (int i = 0; i < matrix.GetLength(0); i++)
            for (int j = 0; j < matrix.GetLength(1); j++)
                SearchMatrix(matrix, used, i, j);

        PrintMarix(matrix);

        Console.WriteLine("String \"{0}\" was found to be longest sequence ({1}) times", maxCountValue, maxCount);
    }

    public static void SearchMatrix(string[,] matrix, bool[, ,] used, int row, int col)
    {
        

        for (int i = 0; i < directions.GetLength(0); i++)
        {
            int checkRow = row;
            int checkCol = col;
            int currentSum = 0;

            if (used[checkRow, checkCol, i])
            {
                continue;
            }

            // if we have the element go same direction to try next and same direction next and ...
            while (IsTraversable(matrix, checkRow, checkCol))
            {

                if (matrix[checkRow, checkCol] == matrix[row, col])
                {
                    currentSum++;
                }

                if (currentSum > maxCount)
                {
                    maxCount = currentSum;
                    maxCountValue = matrix[checkRow, checkCol];
                }

                used[checkRow, checkCol, i] = true;

                checkRow += directions[i, 0];
                checkCol += directions[i, 1];

            }
        }
    }

    private static bool IsTraversable(string[,] martix, int row, int col)
    {
        bool shallYouPass = false;

        if (row >=0 && row < martix.GetLength(0) && col >=0 && col < martix.GetLength(1))
        {
            shallYouPass = true;
        }
        return shallYouPass;
    }

    private static void PrintMarix(string[,] matrix)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write("{0,-3} ", matrix[i, j]);
            }
            Console.WriteLine();
        }
    }
}

